#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:jidi
@file:portal_http_api.py
@time:2022/08/15
"""
import requests

from homework.litemall.model.portal import Portal
from homework.litemall.model.goods import Goods
from homework.litemall.model.shoppingcard import ShoppingCard
from service.wework.utils.log import log


class PortalHttpApi(Portal):
    def search_goods(self, goods: Goods):
        r = requests.get(
            'https://litemall.hogwarts.ceshiren.com/wx/goods/list',
            headers={
                'X-Litemall-Token': self.session.get_token()
            },
            json={
                'keyword': goods.keyword,
                'page': goods.page,
                'limit': goods.limit
            }
        )
        # log.debug("返回值: ", (eval(r.data))['list'])
        return r

    def get_goods(self, goods_id: int):
        r = requests.get(
            'https://litemall.hogwarts.ceshiren.com/wx/goods/detail',
            headers={
                'X-Litemall-Token': self.session.get_token()
            },
            params={
                'id': goods_id
            }
        )
        return r

    def add_goods(self, sc: ShoppingCard):
        r = requests.post(
            'https://litemall.hogwarts.ceshiren.com/wx/cart/add',
            headers={
                'X-Litemall-Token': self.session.get_token()
            },
            json={
                'goodsId': sc.goodsId,
                'number': sc.number,
                'productId': sc.productId
            }
        )
        log.debug(r)
        return r

    def card_status(self):
        r = requests.get(
            'https://litemall.hogwarts.ceshiren.com/wx/cart/index',
            headers={
                'X-Litemall-Token': self.session.get_token()
            },
        )
        log.debug(r)
        return r
